package com.jshcbd.modules.asset.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jshcbd.framework.vo.Result;
import com.jshcbd.framework.query.QueryGenerator;
import com.jshcbd.modules.asset.entity.RepairEntity;
import com.jshcbd.modules.asset.service.IRepairService;
import com.jshcbd.modules.asset.vo.AssetRepairVo;
import com.jshcbd.modules.asset.vo.RepairVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author zhoucd
 * @date
 * @description
 */
@Slf4j
@Api(tags="资产维修")
@RestController
@RequestMapping("/repair")
public class RepairController {

    @Autowired
    private IRepairService repairService;


    @ApiOperation("资产维修列表")
    @RequestMapping(value = {"/list","/list/{status}"}, method = RequestMethod.GET)
    public Result<IPage<RepairVo>> queryPageList(RepairVo repairVo,
                                                 @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                 @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                                 @PathVariable(required = false) Integer status,
                                                 HttpServletRequest req) {
        Result<IPage<RepairVo>> result = new Result();
        QueryWrapper<RepairVo> queryWrapper = QueryGenerator.initQueryWrapper(repairVo, req.getParameterMap());
        if(status != null){
            queryWrapper.eq("repair_state",status);
        }
        Page<RepairVo> page = new Page(pageNo, pageSize);
        IPage<RepairVo> pageList = repairService.listView(page, queryWrapper);
        result.setSuccess(true);
        result.setData(pageList);
        log.info(pageList.toString());
        return result;
    }


    @ApiOperation("添加维修记录")
    @PostMapping("/add")
    public Result add(@RequestBody RepairEntity repairEntity) {
        try {
            repairService.save(repairEntity);
            return Result.ok("添加成功！");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("操作失败");
        }
    }


    @ApiOperation("修改维修记录")
    @PutMapping("/edit")
    public Result edit(@RequestBody RepairEntity repairEntity) {
        try {
            RepairEntity entity = repairService.getById(repairEntity.getId());
            if(entity==null) {
                return Result.error("未找到对应实体");
            }else {
                repairService.updateById(repairEntity);
                return Result.ok("修改成功!");
            }
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error("操作失败");
        }
    }

    @ApiOperation("删除维护记录")
    @DeleteMapping("/delete")
    public Result delete(@RequestParam String id) {
        try {
            this.repairService.removeById(id);
            return Result.ok("删除成功");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            return Result.error(e.getMessage());
        }
    }

    @ApiOperation("资产维修记录")
    @GetMapping(value = {"/listByMainId/{assetId}","listByMainId"} )
    public Result<IPage<AssetRepairVo>> listByMainId(@PathVariable(required = false)  String assetId, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                                   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize, HttpServletRequest req) {
        Result<IPage<AssetRepairVo>> result = new Result();
        Page<AssetRepairVo> page = new Page(pageNo, pageSize);
        IPage<AssetRepairVo> pageList = repairService.pageViewByAssetId(page,assetId);
        result.setData(pageList);
        return result;
    }

}
